package org.jeecg.modules.controller.MedicalRecords;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.patientImages.entity.PatientImages;
import org.jeecg.common.modules.patientImages.service.IPatientImagesService;
import org.jeecg.common.modules.patientMedicalRecords.entity.PatientMedicalRecords;
import org.jeecg.common.modules.patientMedicalRecords.service.IPatientMedicalRecordsService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
* @Description: 病历档案表
* @Author: jeecg-boot
* @Date:   2021-11-13
 * @Version: V1.0
*/
@Api(tags="病历档案表")
@RestController
@RequestMapping("/api/v1/patientMedicalRecords")
@Slf4j
public class MedicalRecordsController extends JeecgController<PatientMedicalRecords, IPatientMedicalRecordsService> {
   @Autowired
   private IPatientMedicalRecordsService patientMedicalRecordsService;
   @Autowired
   private IPatientImagesService patientImagesService;

    /**
     * 分页列表查询
     *
     * @param patientMedicalRecords
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "病历档案表-分页列表查询")
    @ApiOperation(value="病历档案表-分页列表查询", notes="病历档案表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(PatientMedicalRecords patientMedicalRecords,
                                   @RequestParam("openid") String openid,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<PatientMedicalRecords> queryWrapper = QueryGenerator.initQueryWrapper(patientMedicalRecords, req.getParameterMap());
        Page<PatientMedicalRecords> page = new Page<PatientMedicalRecords>(pageNo, pageSize);
        queryWrapper.eq("openid",openid);
        IPage<PatientMedicalRecords> pageList = patientMedicalRecordsService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

   /**
    *   添加
    *
    * @param
    * @return
    */
   @AutoLog(value = "病历档案表-添加")
   @ApiOperation(value="病历档案表-添加", notes="病历档案表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody PatientMedicalRecordsVO patientMedicalRecordsVO) {
       PatientMedicalRecords patientMedicalRecords = new PatientMedicalRecords();
       String medicalId = IdUtil.simpleUUID();
      BeanUtils.copyProperties(patientMedicalRecordsVO,patientMedicalRecords);
       patientMedicalRecords.setId(medicalId);
       patientMedicalRecords.setImagesId(medicalId);
       patientMedicalRecordsService.save(patientMedicalRecords);
//       String images =  patientMedicalRecordsVO.getImages();
//       if(images != null){
       PatientImages patientImages = new  PatientImages();
       patientImages.setId(medicalId);
       patientImages.setImageUrl1(patientMedicalRecordsVO.getImageUrl1() != null?patientMedicalRecordsVO.getImageUrl1() :null);
       patientImages.setImageUrl2(patientMedicalRecordsVO.getImageUrl2() != null?patientMedicalRecordsVO.getImageUrl2() :null);
       patientImages.setImageUrl3(patientMedicalRecordsVO.getImageUrl3() != null?patientMedicalRecordsVO.getImageUrl3() :null);
       patientImages.setImageUrl4(patientMedicalRecordsVO.getImageUrl4() != null?patientMedicalRecordsVO.getImageUrl4() :null);
       patientImages.setImageUrl5(patientMedicalRecordsVO.getImageUrl5() != null?patientMedicalRecordsVO.getImageUrl5() :null);
       patientImages.setImageUrl6(patientMedicalRecordsVO.getImageUrl6() != null?patientMedicalRecordsVO.getImageUrl6() :null);
       patientImages.setImageUrl7(patientMedicalRecordsVO.getImageUrl7() != null?patientMedicalRecordsVO.getImageUrl7() :null);
       patientImages.setImageUrl8(patientMedicalRecordsVO.getImageUrl8() != null?patientMedicalRecordsVO.getImageUrl8() :null);
       patientImages.setImageUrl9(patientMedicalRecordsVO.getImageUrl9() != null?patientMedicalRecordsVO.getImageUrl9() :null);
       patientImages.setPatientMedicalRecordsId(medicalId);
       patientImagesService.save(patientImages);
//       }

       return Result.ok(patientMedicalRecords);
   }

   /**
    *  编辑
    *
    * @param patientMedicalRecords
    * @return
    */
   @AutoLog(value = "病历档案表-编辑")
   @ApiOperation(value="病历档案表-编辑", notes="病历档案表-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody PatientMedicalRecords patientMedicalRecords) {
       patientMedicalRecordsService.updateById(patientMedicalRecords);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "病历档案表-通过id删除")
   @ApiOperation(value="病历档案表-通过id删除", notes="病历档案表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       patientMedicalRecordsService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "病历档案表-批量删除")
   @ApiOperation(value="病历档案表-批量删除", notes="病历档案表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.patientMedicalRecordsService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "病历档案表-通过id查询")
   @ApiOperation(value="病历档案表-通过id查询", notes="病历档案表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       PatientMedicalRecordsVO vo = new PatientMedicalRecordsVO();
       PatientMedicalRecords patientMedicalRecords = patientMedicalRecordsService.getById(id);
       PatientImages patientImages = patientImagesService.getById(patientMedicalRecords.getId());
       if(patientImages == null){
           return Result.ok(patientMedicalRecords);
       }
       BeanUtils.copyProperties(patientMedicalRecords,vo);
       vo.setImageUrl1(patientImages.getImageUrl1());
       vo.setImageUrl2(patientImages.getImageUrl2());
       vo.setImageUrl3(patientImages.getImageUrl3());
       vo.setImageUrl4(patientImages.getImageUrl4());
       vo.setImageUrl5(patientImages.getImageUrl5());
       vo.setImageUrl6(patientImages.getImageUrl6());
       vo.setImageUrl7(patientImages.getImageUrl7());
       vo.setImageUrl8(patientImages.getImageUrl8());
       vo.setImageUrl9(patientImages.getImageUrl9());
       return Result.ok(vo);
   }


}
